package com.app.service.authority;



import java.util.List;


import com.app.entity.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.app.mapper.authority.MenuMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MenuService {

	private final static Logger logger = LoggerFactory.getLogger(MenuService.class);
	@Autowired
	MenuMapper menuMapper;

     public List<Menu> getInfo(String id){
    	 return menuMapper.selectMenu(id);
     }
     public List<Menu> getInfoById(String id){
     	return menuMapper.selectMenuById(id);
	 }

	public List<Menu> queryMenuByParentId(String parent_id) {

		logger.info("func:queryMenuByParentId   %parent_id:"+parent_id);
		return menuMapper.selectMenuByParentId(parent_id);
	}


	public int insertMenu(Menu _menu) {
		int count = menuMapper.selectCountByParId(_menu)+1;
		String sortId = Integer.toString(count);
		_menu.setSortId(sortId);
		logger.info("func:insertMenu%n_department:"+_menu.toString());
		return menuMapper.insertMenu(_menu);
	}


	public  List<Menu> querySecurityMenuAll(String  userId) {

		logger.info("func:insertMenu%nparntId:"+userId);
		List<Menu> dList = menuMapper.querySecurityMenuTreeList(userId);
		return  dList;
	}
	public  List<Menu> queryMenuAll(String  parentId) {

		logger.info("func:insertMenu%nparntId:"+parentId);
		List<Menu> dList = menuMapper.queryMenuTreeList(parentId);
		return  dList;
	}




    public  List<Menu> validateLevelCode(Menu _menu){
		logger.info("func:validateId%n _menu:"+  _menu );
		List<Menu> dList = menuMapper.validateLevelCode(    _menu);
		return  dList;
	}

	//updateDepart
	public  int updateMenu(Menu _menu){
		logger.info("func:updateMenu%n _menu:"+  _menu );
		return menuMapper.updateMenu(    _menu);

	}
	//delDepart
	public  int delMenu(Menu _menu){
		logger.info("func:delMenu%n _menu:"+  _menu );
		return menuMapper.delMenu(    _menu);

	}

	/**
	 * 根据人员id查角色id,可能有多个
	 * @param userId
	 * @return
	 */
    public  List<String> selectRoleId(String userId){
        logger.info("func:selectRoleId%n userId:"+  userId );
        return menuMapper.selectRoleId( userId);

    }

	@Transactional
	public void updateSort(Menu menu) {

		logger.info("func:updateSort");
		String oldIndex = menuMapper.selectOldIndex(menu);
		menu.setSortId(oldIndex);
		menuMapper.updateMenuSortIndex(menu);
		menuMapper.updateMenuSortLast(menu);
	}
}
